package com.qyer.flume.schedule;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * User: Z J Wu Date: 2015/05/15 Time: 10:12 Package: com.qyer.flume.schedule
 */
public class GenericSchedulerHolder {

  private static final Logger LOGGER = LoggerFactory.getLogger(GenericSchedulerHolder.class);

  private static class InnerHolder {
    private static final GenericSchedulerHolder INSTANCE = new GenericSchedulerHolder();
  }

  private GenericSchedulerHolder() {
    SchedulerFactory schedulerFactory = new StdSchedulerFactory();
    try {
      scheduler = schedulerFactory.getScheduler();
    } catch (SchedulerException e) {
      LOGGER.error("Cannot init scheduler.", e);
    }
    try {
      scheduler.start();
    } catch (SchedulerException e) {
      LOGGER.error("Cannot start scheduler.", e);
    }
  }

  public static final GenericSchedulerHolder getInstance() {
    return InnerHolder.INSTANCE;
  }

  private Scheduler scheduler;

  public Scheduler getScheduler() {
    return scheduler;
  }

  public void destory() throws SchedulerException {
    if (scheduler.isShutdown()) {
      return;
    }
    scheduler.shutdown(false);
  }
}
